package com.davidhigenbottam.HeatMap.client;

import com.davidhigenbottam.HeatMap.shared.Institution;
import com.davidhigenbottam.HeatMap.shared.Province;
import com.davidhigenbottam.HeatMap.shared.Query;
import com.davidhigenbottam.HeatMap.shared.Results;
import com.google.gwt.view.client.ListDataProvider;

public class OverlayData {
	
	KmlLayer kmlLayer= null;
	InstitutionsLayer institutionsLayer= null;
	Query query;
	ProvinceTable provinceTable;
	InstitutionTable institutionTable;

	public OverlayData(Query q) {
		query = q;

		provinceTable=  new ProvinceTable();
		provinceTable.setQuery(q);
		
		institutionTable=  new InstitutionTable();
		institutionTable.setQuery(q);		
	}


	public ProvinceTable getProvinceTable() {
		return provinceTable;
	}



	public void setProvinceTable(ProvinceTable provinceTable) {
		this.provinceTable = provinceTable;
	}



	public InstitutionTable getInstitutionTable() {
		return institutionTable;
	}


	public void setInstitutionTable(InstitutionTable institutionTable) {
		this.institutionTable = institutionTable;
	}


	public boolean isHidden() {
		if(kmlLayer==null)
			return false;
		return kmlLayer.isHidden();
	}

	public void show() {
		provinceTable.show();
		institutionTable.show();
		if(kmlLayer!=null)
			kmlLayer.setVisible(true);
		if(institutionsLayer!=null)
			institutionsLayer.setVisible(true);
		
		
	}

	public void hide() {
		provinceTable.hide();
		institutionTable.hide();
		if(kmlLayer!=null)
			kmlLayer.setVisible(false);
		if(institutionsLayer!=null)
			institutionsLayer.setVisible(false);
	}

	public KmlLayer getOverlay() {
		return kmlLayer;
	}

	public void setOverlay(KmlLayer overlay) {
		this.kmlLayer = overlay;
	}

	public Query getQuery() {
		return query;
	}

	public void setQuery(Query query) {
		this.query = query;
	}


	public InstitutionsLayer getInstitutionsLayer() {
		return institutionsLayer;
	}


	public void setInstitutionsLayer(InstitutionsLayer institutionsLayer) {
		this.institutionsLayer = institutionsLayer;
	}


	public void addResults(Results results) {
		if(results.getProvinces()!=null){
			ProvinceTable pt= getProvinceTable();
			
			ListDataProvider<Province> dp= pt.getDataProvider();
			dp.setList(results.getProvinces());
			
			UI.getInstance().addProvinceTable(pt);
			pt.show();
			
			pt.addSortHandlers();
		}
		if(results.getInstitutions()!=null){
			InstitutionTable it= getInstitutionTable();
			
			ListDataProvider<Institution> dp= it.getDataProvider();
			dp.setList(results.getInstitutions());
			
			UI.getInstance().addInstitutionTable(it);
			it.show();
			
			it.addSortHandlers();
		}		
		
	}
	
//	public LatLng getCenter(){
//		if(this.getOverlay()==null)
//			return UI.getInstance().mapDefaultCenter;
//		return this.getOverlay().getDefaultCenter();
//	}
	

}
